package com.szqhyxj.youxiangju;

import android.util.Log;

import com.szqhyxj.youxiangju.sys.BaseResult;

import io.reactivex.functions.Consumer;

public abstract class BaseConsumer<T> implements Consumer<BaseResult<T>> {

    private static final String TAG = BaseConsumer.class.getSimpleName();

    @Override
    public void accept(BaseResult<T> baseResult) throws NoLoginException, BusinessLogicException {
        if (baseResult.businessLogicSuccess()) {
            successAccept(baseResult.getData());
            return;
        }

        if (baseResult.tokenNoAvailable()) {
            throw new NoLoginException(baseResult.getMessage(), new Throwable());
        }

        if (baseResult.businessLogicFail()) {
            //后台结果失败业务逻辑处理
            error(baseResult.getCode(),baseResult.getMessage());
            //后台结果失败抛出异常，全局toash
            if(baseResult.getMessage().contains("最新版本")){

            }else {
                throw new BusinessLogicException(baseResult.getMessage(), new Throwable());
            }

        }

        Log.i(TAG, "accept: " + baseResult.toString());
    }

    protected abstract void error(String code,String msg);

    protected abstract void successAccept(T t);
}